<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="gtitle" style="margin-bottom: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>告警方式配置</div>

      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2"><a-checkbox v-model="emailAlarmConfig.available" @change="updateemailAlarmconfig('available',emailAlarmConfig.available)"></a-checkbox>&nbsp;&nbsp;邮件告警:</a-col>
          <a-col :span="22">开启邮件告警后，系统会将资产相关联的告警数据自动推送给已经填写电子邮箱的用户，全局管理员默认收到所有告警邮件</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">邮件服务器地址:</a-col>
          <a-col :span="4"><a-input v-model="emailAlarmConfig.emailServerUrl" @blur="updateemailAlarmconfig('emailServerUrl',emailAlarmConfig.emailServerUrl)"></a-input></a-col>
          <a-col :span="2" :offset="4">端口:</a-col>
          <a-col :span="4"><a-input  v-model="emailAlarmConfig.port" @blur="updateemailAlarmconfig('port',emailAlarmConfig.port)"></a-input></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">发件人:</a-col>
          <a-col :span="4"><a-input v-model="emailAlarmConfig.fromUser" @blur="updateemailAlarmconfig('fromUser',emailAlarmConfig.fromUser)"></a-input></a-col>
          <a-col :span="2" :offset="4">加密类型:</a-col>
          <a-col :span="4">
            <a-select style="width: 100%" v-model="emailAlarmConfig.encryptionType" @change="updateemailAlarmconfig('encryptionType',emailAlarmConfig.encryptionType)">
              <a-select-option value="SSL/TLS">SSL/TLS</a-select-option>
              <a-select-option value="STARTTLS">STARTTLS</a-select-option>
              <a-select-option value="NONE">NONE</a-select-option>
            </a-select>
          </a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">密码:</a-col>
          <a-col :span="4"><a-input-password  v-model="emailAlarmConfig.password" @blur="updateemailAlarmconfig('password',emailAlarmConfig.password)"></a-input-password></a-col>
          <a-col :span="2" :offset="4">测试收件人:</a-col>
          <a-col :span="4"><a-input v-model="emailAlarmConfig.toUser" @blur="updateemailAlarmconfig('toUser',emailAlarmConfig.toUser)"></a-input></a-col>
          <a-col :span="2" :offset="1"><a-button type="primary">测试</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2"><a-checkbox v-model="syslogAlarmConfig.available" @change="updatesyslogAlarmconfig('available',syslogAlarmConfig.available)"></a-checkbox>&nbsp;&nbsp;syslog:</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">服务器地址:</a-col>
          <a-col :span="4"><a-input v-model="syslogAlarmConfig.syslogServerUrl" @blur="updatesyslogAlarmconfig('syslogServerUrl',syslogAlarmConfig.syslogServerUrl)"></a-input></a-col>
          <a-col :span="2" :offset="4">端口:</a-col>
          <a-col :span="4"><a-input v-model="syslogAlarmConfig.port" @blur="updatesyslogAlarmconfig('port',syslogAlarmConfig.port)"></a-input></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">传输类型:</a-col>
          <a-col :span="4">
            <a-select style="width: 100%"  v-model="syslogAlarmConfig.transmissionType" @change="updatesyslogAlarmconfig('transmissionType',syslogAlarmConfig.transmissionType)">
              <a-select-option value="UDP">UDP</a-select-option>
              <a-select-option value="TCP">TCP</a-select-option>
            </a-select>
          </a-col>
          <a-col :span="2" :offset="4"></a-col>
          <a-col :span="4"></a-col>
          <a-col :span="2" :offset="1"><a-button type="primary">测试</a-button></a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>告警方式配置</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="6">
            <a-row class='rowmb'>选择需要告警的内容：</a-row>
            <a-checkbox-group v-model="choiceAlarmContent" @change="updatechoiceAlarmContent()">
              <a-row class='rowmb'><a-checkbox value="1"></a-checkbox>系统登录错误日志</a-row>
              <a-row class='rowmb'><a-checkbox value="2"></a-checkbox>补丁库更新日志</a-row>
              <a-row class='rowmb'><a-checkbox value="3"></a-checkbox>补丁转正日志</a-row>
              <a-row class='rowmb'><a-checkbox value="4"></a-checkbox>存在高危漏洞的主机总量信息</a-row>
              <a-row class='rowmb'><a-checkbox value="5"></a-checkbox>离线超过 <a-input style="width: 50px;"></a-input> 天的主机数量</a-row>
              <a-row class='rowmb'><a-checkbox value="6"></a-checkbox>授权到期提醒</a-row>
            </a-checkbox-group>
          </a-col>
          <a-col :span="6">
            <a-row class='rowmb'>选择实时告警的类型：</a-row>
            <a-checkbox-group v-model="realTimeAlarmType" @change="updaterealTimeAlarmType()">
              <a-row class='rowmb'><a-checkbox value="1"></a-checkbox>系统登录错误日志</a-row>
              <a-row class='rowmb'><a-checkbox value="2"></a-checkbox>补丁库更新日志</a-row>
              <a-row class='rowmb'><a-checkbox value="3"></a-checkbox>补丁转正日志</a-row>
            </a-checkbox-group>
          </a-col>
          <a-col :span="6">
            <a-row class='rowmb'>选择定时告警的内容：</a-row>
            <a-checkbox-group v-model="timingAlarmContent" @change="updatetimingAlarmContent()">
              <a-row class='rowmb'><a-checkbox value="1"></a-checkbox>系统登录错误日志</a-row>
              <a-row class='rowmb'><a-checkbox value="2"></a-checkbox>补丁库更新日志</a-row>
              <a-row class='rowmb'><a-checkbox value="3"></a-checkbox>补丁转正日志</a-row>
              <a-row class='rowmb'><a-checkbox value="4"></a-checkbox>存在高危漏洞的主机总量信息</a-row>
              <a-row class='rowmb'><a-checkbox value="5"></a-checkbox>离线主机数量</a-row>
              <a-row class='rowmb'><a-checkbox value="6"></a-checkbox>授权到期提醒</a-row>
            </a-checkbox-group>
          </a-col>
          <a-col :span="6">
            <a-row class='rowmb'>定时设置：</a-row>
            <a-radio-group style="width: 100%;"  v-model="timingSetting.method" @change="updatetimeseting()">
              <a-row class='rowmb'>
                <a-col :span="2"><a-radio :value="1"></a-radio></a-col>
                <a-col :span="6">间隔告警：</a-col>
                <a-col :span="16">每间隔 <a-input style="width: 50px" v-model="timingSetting.intervaltime" @blur="updatetimeseting()"></a-input> 分钟一次</a-col>
              </a-row>
              <a-row class='rowmb'>
                <a-col :span="2"><a-radio :value="2"></a-radio></a-col>
                <a-col :span="6">定时探测：</a-col>
                <a-col :span="16">定时方式
                  <a-select style="width: 120px" v-model="timingSetting.intervalmode"  @change="updatetimeseting()" >
                    <a-select-option value="3">每天一次</a-select-option>
                    <a-select-option value="2">每周一次</a-select-option>
                    <a-select-option value="1">每月一次</a-select-option>
                  </a-select>
                </a-col>
              </a-row>
              <a-row class='rowmb' v-if="timingSetting.intervalmode == 1">
                <a-col :span="2"></a-col>
                <a-col :span="6"></a-col>
                <a-col :span="16">选择日期
                  <a-select style="width: 120px;"  v-model="timingSetting.day"  @change="updatetimeseting()">
                    <a-select-option v-for=" i in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]" :value="i">
                      {{i}}</a-select-option>
                  </a-select>
                </a-col>
              </a-row>
              <a-row class='rowmb' v-if="timingSetting.intervalmode == 2">
                <a-col :span="2"></a-col>
                <a-col :span="6"></a-col>
                <a-col :span="16">选择日期
                  <a-select style="width: 120px;"  v-model="timingSetting.week"  @change="updatetimeseting()">
                    <a-select-option value="1">星期一</a-select-option>
                    <a-select-option value="2">星期二</a-select-option>
                    <a-select-option value="3">星期三</a-select-option>
                    <a-select-option value="4">星期四</a-select-option>
                    <a-select-option value="5">星期五</a-select-option>
                    <a-select-option value="6">星期六</a-select-option>
                    <a-select-option value="7">星期日</a-select-option>
                  </a-select>
                </a-col>
              </a-row>
              <a-row class='rowmb'>
                <a-col :span="2"></a-col>
                <a-col :span="6"></a-col>
                <a-col :span="16">选择时间 <a-time-picker format="HH:mm:ss" v-model="time" @change="updatetime()"/></a-col>
              </a-row>
            </a-radio-group>
          </a-col>
        </a-row>
      </div>

    </a-card>
  </page-header-wrapper>
</template>

<script>
  import moment from 'moment'
  import { getalarmconfig,updatealarmconfig } from '@/api/warn'
  import {} from '../../utils/constant'
  import TagSelectOption from '@/components/TagSelect/TagSelectOption'
  export default {
    name: 'PatchUpdateManage',
    components: { TagSelectOption },
    data() {
      return {
        intervalmode:'',
        emailAlarmConfig:{},
        syslogAlarmConfig:{},
        choiceAlarmContent:[],
        realTimeAlarmType:[],
        timingAlarmContent:[],
        timingSetting:{},
        time:''
      }
    },
    mounted() {
      this.$info(
        {
          title:'提示',
          content:'测试Lisence未授权！',
          onOk:()=> {
            this.$router.push({ path: '/overview/overview' })
          },
        }
      )
      this.getalarm()
    },
    methods:{
      updatetime(){
        this.timingSetting.time = moment(this.time).format('HH:mm:ss')
        this.updatetimeseting()
      },
      updatetimeseting(){
        updatealarmconfig({
          id:1,
          timingSetting:JSON.stringify(this.timingSetting)
        }).then(res=>{})
      },
      getalarm(){
        getalarmconfig({id:1}).then(res=>{
          let email = JSON.parse(res.data.emailAlarmConfig)
          email.available = email.available==1?true:false
          this.emailAlarmConfig = email

          let alarm = JSON.parse(res.data.syslogAlarmConfig)
          alarm.available = alarm.available==1?true:false
          this.syslogAlarmConfig = alarm

          if(res.data.choiceAlarmContent){
            this.choiceAlarmContent = res.data.choiceAlarmContent.split(',')
          }
          if(res.data.realTimeAlarmType){
            this.realTimeAlarmType = res.data.realTimeAlarmType.split(',')
          }
          if(res.data.timingAlarmContent){
            this.timingAlarmContent = res.data.timingAlarmContent.split(',')
          }
          if(res.data.timingSetting){
            this.timingSetting = JSON.parse(res.data.timingSetting)
            if(this.timingSetting.time){
              this.time = moment(this.timingSetting.time,'HH:mm:ss')
            }
          }
        })
      },
      updatetimingAlarmContent(){
        updatealarmconfig({
          id:1,
          timingAlarmContent:this.timingAlarmContent.join(',')
        }).then(res=>{})
      },
      updaterealTimeAlarmType(){
        updatealarmconfig({
          id:1,
          realTimeAlarmType:this.realTimeAlarmType.join(',')
        }).then(res=>{})
      },
      updatechoiceAlarmContent(){
        updatealarmconfig({
          id:1,
          choiceAlarmContent:this.choiceAlarmContent.join(',')
         }).then(res=>{})
      },
      updateemailAlarmconfig(key,value){
        let  temp= {}
        Object.assign(temp,this.emailAlarmConfig)
        let json = {}
        json.id = 1
        if(key == 'available'){
          value = value?1:0
        }
        temp[key] = value
        json.emailAlarmConfig = JSON.stringify(temp)
        updatealarmconfig(json).then(res=>{})
      },

      updatesyslogAlarmconfig(key,value){
        let  temp= {}
        Object.assign(temp,this.syslogAlarmConfig)
        let json = {}
        json.id = 1
        if(key == 'available'){
          value = value?1:0
        }
        temp[key] = value
        json.syslogAlarmConfig = JSON.stringify(temp)
        updatealarmconfig(json).then(res=>{})
      },
    }
  }
</script>

<style lang="less"  scoped>

  .gtitle{
    background-color: #e6f3fc;
    line-height: 40px;
    padding-left: 20px;
    color: #333;
    font-weight: bold;

    i{
      margin-right: 10px;
    }
  }
  .rowtxt,.ant-radio-group{
    line-height: 32px;

  }
  .ant-radio-group{
    width: 100%;
  }
  .rowspan{
    display: inline-block;
    width: 150px;
  }
  .showtxt{
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 0 10px;
    height: 120px;
    overflow-y: auto;
  }
  .rowmb{
    margin-bottom: 10px;
  }
  .blue{
    color: #3366FF;
  }
  .iplist{
    border: 1px solid #ddd;
    padding: 5px;
    min-height: 100px;
    line-height: 30px;
    div:hover{
      background-color: #efefef;
    }
    .icon{
      color: red;
      cursor: pointer;
      line-height: 30px;
      float: right;
      margin-right: 20px;
    }
  }
  .rowmb .ant-checkbox-wrapper{
    margin-right: 10px;
  }
</style>
